package com.log4jviewer.ui.preferences.filters;

import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;
import org.junit.Ignore;
import org.junit.Test;

/**
 * 
 * @author <a href="mailto:Daniil.Yaroslavtsev@gmail.com"> Daniil Yaroslavtsev </a>
 * 
 */
public class FilterTableTest extends AbstractFiltersPageTest {

    protected static final SWTWorkbenchBot bot = getBot();

    private FilterContentProvider filterContentProvider = getFilterContentProvider();
    private FilterSettings filterSettings = getFilterSettings();

    private TableViewer filterSettingsViewer = filterSettings.getTableViewer();

    /**
     * Tests that active filter index changes when the FilterTable selection changed by mouseClick.
     * 
     * @throws InterruptedException
     */
    @Test
    public void testFilterTableSelectionChangedOnMouseClick() {
        addFilters(3);
        getFilterTable().select(0);
        int selectedFilterIndexFirst = filterContentProvider.getActiveFilterIndex();
        getFilterTable().select(1);
        int selectedFilterIndexSecond = filterContentProvider.getActiveFilterIndex();
        assertTrue(selectedFilterIndexFirst == 0);
        assertTrue(selectedFilterIndexSecond == 1);
    }

    /**
     * Tests that active filter index changes when FilterTable selection changing (by arrow_down key).
     * 
     * @throws InterruptedException
     */
    @Test
    public void testFilterTableSelectionChangedByArrowDownPressing() {
        addFilters(3);
        getFilterTable().select(0);
        getFilterTable().pressShortcut(getKey(SWT.ARROW_DOWN));
        bot.sleep(200);
        int selectedFilterIndexSecond = filterContentProvider.getActiveFilterIndex();
        assertTrue(selectedFilterIndexSecond == 1);
    }

    /**
     * Tests that FilterTable cells can be edit (by F2 key).
     * 
     * @throws InterruptedException
     */
    @Test
    public void testFilterTableEditingByF2Key() {
        addFilter();
        getFilterTable().click(0, 0);
        // try to edit first cell by F2 key
        getFilterTable().pressShortcut(getKey(SWT.F2));
        waitForCellEditorState(filterSettingsViewer, true);
        boolean isCellEditorActive = filterSettingsViewer.isCellEditorActive();
        assertTrue(isCellEditorActive);
    }

    /**
     * Tests that FilterTable cells can be edit (by mouse doubleClick).
     * 
     */
    @Test
    public void testFilterTableEditingByMouseDoubleClick() {
        addFilter();
        // try to edit first cell by mouse doubleclick
        getFilterTable().doubleClick(0, 0);
        waitForCellEditorState(filterSettingsViewer, true);
        boolean isCellEditorActive = filterSettingsViewer.isCellEditorActive();
        assertTrue(isCellEditorActive);
    }

    /**
     * Tests that all FilterTable ENABLE_COLUMN`s cells can`t be edit (by F2 key or mouse doubleClick).
     * 
     * @throws InterruptedException
     */
    @Test
    public void testFilterTableEnableColumnIsNotEditable() throws InterruptedException {

        addFilter();

        int enableColumnIndex = filterSettings.ENABLE_COLUMN_INDEX;

        for (int i = 0; i < getFilterTable().rowCount(); i++) {
            // try to edit ENABLE_COLUMN`s cell by F2 key
            getFilterTable().click(i, enableColumnIndex);
            getFilterTable().pressShortcut(getKey(SWT.F2));
            waitForCellEditorState(filterSettingsViewer, false);
            boolean isCellEditorActive = filterSettingsViewer.isCellEditorActive();
            assertFalse(isCellEditorActive);

            // try to edit ENABLE_COLUMN`s cell by mouse doubleclick
            getFilterTable().doubleClick(i, enableColumnIndex);
            waitForCellEditorState(filterSettingsViewer, false);
            isCellEditorActive = filterSettingsViewer.isCellEditorActive();
            assertFalse(isCellEditorActive);
        }
    }

    @Ignore
    @Test
    public void testInsertAndDelKeyJob() throws ParseException {

        SWTBotTable filterTable = getFilterTable();

        // setPlaybackDelay(200);

        addFilter();

        filterTable.click(0, 0);

        // TODO: bot can`t push the SWT.Insert key while running this test. It is very disheartening.
        for (int i = 0; i < 3; i++) {
            filterTable.pressShortcut(getKey(SWT.INSERT));
        }

        boolean insertionByKeyWorks = (filterTable.rowCount() == 4);

        for (int i = 0; i < 4; i++) {
            // filterTable.getTableItem(0).select(); // select a first filter
            filterTable.pressShortcut(getKey(SWT.DEL));
        }

        boolean delitionByKeyWorks = (filterTable.rowCount() == 0);

        // setPlaybackDelay(0);

        assertTrue(insertionByKeyWorks);
        assertTrue(delitionByKeyWorks);
    }

}
